package com.touchstone.synchdata.common.enums;



import com.touchstone.synchdata.common.dto.OptionDTO;
import com.touchstone.synchdata.common.exception.BusinessException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 返回状态枚举类
 * @Author : hunter
 * @Date: 2019-06-28 15:45
 * @Version: 1.0
 **/
public enum ResultEnum {

    /**
     * 操作成功
     */
    SUCCESS(200, "操作成功"),
    /**
     * 操作失败
     */
    FAIL(400, "操作失败"),
    NO_TOKEN(401, "无token"),
    TOKEN_INVALID(402, "token无效"),
    NO_PERMISSION(403, "无此权限"),
    NO_FOUND(404, "你输入的路径不存在"),
    NO_RECORD(405, "未查询到记录"),
    NO_MATCH(406, "输入的格式不正确"),
    NO_LOGIN(407, "(公众号)用户未登录"),
    TOKEN_REFRESH(408,"刷新token"),
    RE_LOGIN(409, "用户信息存在异常"),
    ERROR(500, "服务器忙,请稍后重试");

    /**
     * 状态码
     */
    private Integer statusCode;

    /**
     * 状态说明
     */
    private String description;

    ResultEnum(Integer statusCode, String description) {
        this.statusCode = statusCode;
        this.description = description;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getDesc() {
        return description;
    }

    public void setDesc(String desc) {
        this.description = desc;
    }

    public static String getDescriptionByCode(Integer statusCode) {
        if (statusCode == null) {
            throw new BusinessException("状态码不能为空");
        }
        for (ResultEnum resultEnum : ResultEnum.values()) {
            if (resultEnum.getStatusCode().equals(statusCode)) {
                return resultEnum.getDesc();
            }
        }
        return "";
    }

    /**
     * 状态列表
     */
    private static List<OptionDTO> optionDTOList = new ArrayList<>(ResultEnum.values().length);

    static {
        OptionDTO optionDTO;
        for (ResultEnum resultEnum : ResultEnum.values()) {
            optionDTO = new OptionDTO(resultEnum.description, resultEnum.statusCode);
            optionDTOList.add(optionDTO);
        }
    }

    /**
     *  返回状态列表
     *
     * @return 状态列表
     * @author hunter
     * @date 2019/7/10 15:56
     **/
    public static List<OptionDTO> getOptionDTOList() {
        return optionDTOList;
    }

}
